package md.mru.frontend.controllers;

import md.mru.core.dtos.FamilyStatusDto;
import md.mru.core.dtos.NationalityDto;
import md.mru.core.dtos.SalaryLevelDto;
import md.mru.core.dtos.SalaryStepDto;
import md.mru.core.service.ApplicationDataService;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 01.04.2014
 */
@ManagedBean(name = "applicationStateBean")
@ApplicationScoped
public class ApplicationStateBean {

    @ManagedProperty(value = "#{applicationDataService}")
    private ApplicationDataService applicationDataService;
    private List<NationalityDto> nationalities;
    private List<FamilyStatusDto> familyStatuses;
    private List<SalaryLevelDto> salaryLevels;
    private List<SalaryStepDto> salarySteps;

    @PostConstruct
    public void initData() {
        this.nationalities = applicationDataService.getNationalities();
        this.familyStatuses = applicationDataService.getFamilyStatuses();
    }

    public String getNationalityById(long id) {
        for (NationalityDto n : nationalities) {
            if (n.getId() == id) {
                return n.getName();
            }
        }
        return null;
    }

    public Long getNationalityByName(String name) {
        for (NationalityDto n : nationalities) {
            if (n.getName().equals(name)) {
                return n.getId();
            }
        }
        return null;
    }

    public String getFamilyStatusById(long id) {
        for (FamilyStatusDto f : familyStatuses) {
            if (f.getId() == id) {
                return f.getName();
            }
        }
        return null;
    }

    public Long getFamilyStatusByName(String name) {
        for (FamilyStatusDto f : familyStatuses) {
            if (f.getName().equals(name)) {
                return f.getId();
            }
        }
        return null;
    }

    public List<SalaryLevelDto> getSalaryLevels() {
        return salaryLevels;
    }

    public List<SalaryStepDto> getSalarySteps() {
        return salarySteps;
    }

    public List<NationalityDto> getNationalities() {
        return nationalities;
    }

    public void setNationalities(List<NationalityDto> nationalities) {
        this.nationalities = nationalities;
    }

    public List<FamilyStatusDto> getFamilyStatuses() {
        return familyStatuses;
    }

    public void setFamilyStatuses(List<FamilyStatusDto> familyStatuses) {
        this.familyStatuses = familyStatuses;
    }

    public void setApplicationDataService(ApplicationDataService applicationDataService) {
        this.applicationDataService = applicationDataService;
    }
}
